package nextoffer;

/**
 * @author ：szq
 * @date ：Created in 2022/3/17 10:51
 * @description：剑指 Offer 25. 合并两个排序的链表
 * @modified By：
 * @version: $
 */
public class mergeTwoSortedLinkedLists {


      public class ListNode {
          int val;
         ListNode next;
         ListNode(int x) { val = x; }
      }

    class Solution {
        public ListNode mergeTwoLists(ListNode l1, ListNode l2) {


            ListNode dummy = new ListNode(-1);
            ListNode head = dummy;
            while(l1!=null&&l2!=null)
            {
                if(l1.val<l2.val)
                {
                    dummy.next = l1;
                    l1 = l1.next;
                }
                else
                {
                    dummy.next = l2;
                    l2 = l2.next;
                }

                dummy = dummy.next;

            }
            dummy.next = l1!=null?l1:l2; //
            return head.next;

        }
    }
}
